<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools 1.3 Documentation - Types/Array.Extras</title>
</head>

<body>
  <div class="doc">
    <div class="methods">
      <h4 class="menu-item"><a href="#Array">Array</a></h4>
      <div class="menu-item"><a href="#Array:min">min</a></div>
      <div class="menu-item"><a href="#Array:max">max</a></div>
      <div class="menu-item"><a href="#Array:average">average</a></div>
      <div class="menu-item"><a href="#Array:shuffle">shuffle</a></div>
      <div class="menu-item"><a href="#Array:sum">sum</a></div>
      <div class="menu-item"><a href="#Array:unique">unique</a></div>
      <div class="menu-item"><a href="#Array:reduce">reduce</a></div>
      <div class="menu-item"><a href="#Array:reduceRight">reduceRight</a></div>
    </div>
    <div class="l-border r-border span-15 content">
      <h1 id="Array"><a href="#Array">Type: Array</a></h1>
      <p class="description">A collection of useful methods to extend Arrays.</p>
      <h3>See Also</h3>
      <ul>
        <li><a href="../../../docs/core/Types/Array.html">Array</a></li>
      </ul>
      <h2 id="Array:min"><a href="#Array:min">Array Method: min</a></h2>
      <p class="description">Calls Math.min on the array and returns its lowest value.</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArray.<span class="me1">min</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="br0">[</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">]</span>.<span class="me1">min</span><span class="br0">(</span><span class="br0">)</span>;
<span class="co1">//returns 1</span>

</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>number</em>) the lowest number in the array</li>
      </ul>
      <h2 id="Array:max"><a href="#Array:max">Array Method: max</a></h2>
      <p class="description">Calls Math.max on the array and returns its highest value.</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArray.<span class="me1">max</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="br0">[</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">]</span>.<span class="me1">max</span><span class="br0">(</span><span class="br0">)</span>;

<span class="co1">//returns 3</span>
</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>number</em>) the highest number in the array</li>
      </ul>
      <h2 id="Array:average"><a href="#Array:average">Array Method: average</a></h2>
      <p class="description">Calculates the average value of the array.</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArray.<span class="me1">average</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="br0">[</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">]</span>.<span class="me1">average</span><span class="br0">(</span><span class="br0">)</span>;

<span class="co1">//returns 2</span>
</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>number</em>) the average value of the array</li>
      </ul>
      <h2 id="Array:shuffle"><a href="#Array:shuffle">Array Method: shuffle</a></h2>
      <p class="description">Randomizes the array (altering it).</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArray.<span class="me1">shuffle</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="br0">[</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">]</span>.<span class="me1">shuffle</span><span class="br0">(</span><span class="br0">)</span>;

</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>array</em>) the array.</li>
      </ul>
      <h3>Note</h3>
      <p>Calling this method alters the array; it doesn't just return a new array with the same contents shuffled. It does, however, return itself.</p>
      <h2 id="Array:sum"><a href="#Array:sum">Array Method: sum</a></h2>
      <p class="description">Sums up all values in an array.</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArray.<span class="me1">sum</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript">$$<span class="br0">(</span><span class="st0">'ul.menu li'</span><span class="br0">)</span>.<span class="me1">getWidth</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">sum</span><span class="br0">(</span><span class="br0">)</span>;
<span class="co1">//returns the width of all li elements inside ul.menu as a sum</span>
</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>number</em>) a number containing the sum of all values in the given array</li>
      </ul>
      <h2 id="Array:unique"><a href="#Array:unique">Array Method: unique</a></h2>
      <p class="description">Returns a new array without duplicate values.</p>
      <h3>Syntax</h3>
      <pre class="javascript">myArrayWithoutDupes = myArray.<span class="me1">unique</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="kw2">var</span> fruits = <span class="br0">[</span><span class="st0">'apple'</span>, <span class="st0">'lemon'</span>, <span class="st0">'pear'</span>, <span class="st0">'lemon'</span>, <span class="st0">'apple'</span><span class="br0">]</span>.<span class="me1">unique</span><span class="br0">(</span><span class="br0">)</span>;

<span class="co1">//fruits == ['apple', 'lemon', 'pear']</span>
</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>array</em>) a new array without duplicates.</li>
      </ul>
      <h2 id="Array:reduce"><a href="#Array:reduce">Array Method: reduce</a></h2>
      <p class="description">Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.</p>
      <h3>Syntax</h3>
      <pre class="javascript">result = myArray.<span class="me1">reduce</span><span class="br0">(</span>fn<span class="br0">[</span>, value<span class="br0">]</span><span class="br0">)</span>;
</pre>
      <h3>Arguments</h3>
      <ol>
        <li>fn - (<em>function</em>) Function to execute on each value in the array.</li>
        <li>value - (<em>mixed</em>) Value to use as the first argument to the first call of the <code>fn</code> </li>
      </ol>
      <h4>Signature:</h4>
      <pre class="javascript">fn<span class="br0">(</span>previousValue, currentValue, index, array<span class="br0">)</span>

</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="br0">[</span><span class="nu0">0</span>, <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span><span class="br0">]</span>.<span class="me1">reduce</span><span class="br0">(</span><span class="kw2">function</span><span class="br0">(</span>a, b<span class="br0">)</span><span class="br0">{</span>

    <span class="kw1">return</span> a + b;
<span class="br0">}</span><span class="br0">)</span>; <span class="co1">// returns 10</span>
&nbsp;
<span class="br0">[</span><span class="nu0">0</span>, <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span><span class="br0">]</span>.<span class="me1">reduce</span><span class="br0">(</span><span class="kw2">function</span><span class="br0">(</span>a, b<span class="br0">)</span><span class="br0">{</span>

    <span class="kw1">return</span> a + b;
<span class="br0">}</span>, <span class="nu0">20</span><span class="br0">)</span>; <span class="co1">// returns 20</span>
</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>mixed</em>) Returns the reduced single value of the array.</li>
      </ul>
      <h3>See also:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce">MDC Array.reduce</a></li>
      </ul>
      <h2 id="Array:reduceRight"><a href="#Array:reduceRight">Array Method: reduceRight</a></h2>
      <p class="description">Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.</p>
      <h3>Syntax</h3>
      <pre class="javascript">result = myArray.<span class="me1">reduceRight</span><span class="br0">(</span><span class="br0">)</span>;
</pre>
      <h3>Arguments</h3>
      <ol>
        <li>fn - (<em>function</em>) Function to execute on each value in the array.</li>
        <li>value - (<em>mixed</em>) Value to use as the first argument to the first call of the <code>fn</code> </li>
      </ol>
      <h4>Signature:</h4>
      <pre class="javascript">fn<span class="br0">(</span>previousValue, currentValue, index, array<span class="br0">)</span>
</pre>
      <h3>Example</h3>
      <pre class="javascript"><span class="kw2">var</span> flattened = <span class="br0">[</span><span class="br0">[</span><span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">]</span>, <span class="br0">[</span><span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">]</span>, <span class="br0">[</span><span class="nu0">4</span>, <span class="nu0">5</span><span class="br0">]</span><span class="br0">]</span>.<span class="me1">reduceRight</span><span class="br0">(</span><span class="kw2">function</span><span class="br0">(</span>a, b<span class="br0">)</span> <span class="br0">{</span>

    <span class="kw1">return</span> a.<span class="me1">concat</span><span class="br0">(</span>b<span class="br0">)</span>;
<span class="br0">}</span>, <span class="br0">[</span><span class="br0">]</span><span class="br0">)</span>;
<span class="co1">// flattened is [4, 5, 2, 3, 0, 1]</span>

</pre>
      <h3>Returns</h3>
      <ul>
        <li>(<em>mixed</em>) Returns the reduced single value of the array.</li>
      </ul>
      <h3>See also:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/ReduceRight">MDC Array.reduceRight</a></li>
      </ul>
    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>
